const path = require("path");

const Koa = require("koa");
const Router = require("koa-router");
const multer = require("koa-multer");

const app = new Koa();
const uploadRouter = new Router({ prefix: "/upload" });

const storage = multer.diskStorage({
  destination: (req, file, callback) => {
    callback(null, "./upload/");
  },
  filename: (req, file, callback) => {
    callback(null, Date.now() + path.extname(file.originalname));
  },
});

// const upload = multer({ dest: "./upload/" });
const upload = multer({
  storage,
});

uploadRouter.post("/avatar", upload.single("avatar"), (ctx, next) => {
  console.log(ctx.req.file);
  ctx.response.body = "文件上传成功";
});

app.use(uploadRouter.routes());

app.listen(8000, () => {
  console.log("文件上传服务器启动成功");
});
